<template>
  <div class="page-layout">
    <el-form :model="formInline" ref="form" label-width="90px" class="form-inline" label-suffix="：">
      <el-row :gutter="10">
        <el-col :lg="6" :xl="6"><el-form-item label="集装箱号">
            <el-input v-model="formInline.containerNumber" placeholder="请输入集装箱号"></el-input> </el-form-item></el-col>
        <!-- <el-col :lg="6" :xl="6"><el-form-item label="运单号">
            <el-input v-model="formInline.tagInvoiceWaybill" placeholder="请输入运单号"></el-input> </el-form-item></el-col> -->
        <el-col :lg="6" :xl="6">
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button :disabled="!multipleSelection.length" @click="payClick">支付</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table ref="table" border @selection-change="handleSelectionChange" :data="tableData" :height="tableHeight" :style="{ width: '100%' }">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="templateName" label="投保名称">
      </el-table-column>
      <el-table-column prop="containerNumber" label="集装箱号" width="140">
      </el-table-column>
      <!-- <el-table-column prop="tagInvoiceWaybill" label="运单号">
      </el-table-column> -->
      <el-table-column prop="insureFee" label="保费（元）" width="110">
      </el-table-column>
      <el-table-column prop="goodsName" label="货物名称">
      </el-table-column>
      <el-table-column prop="startTime" label="启运日期">
      </el-table-column>
      <el-table-column prop="startPlace" label="启运地">
      </el-table-column>
      <el-table-column prop="endPlace" label="目的地">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <!-- <el-button @click="paymentClick(scope.row)" type="text" size="small">支付</el-button> -->
          <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 10px" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <el-dialog title="支付" :visible.sync="payVisible" width="30%" :before-close="() => payVisible = false" v-loading="loading">
      <el-form :model="payForm" :rules="payRules" ref="payForm" label-width="90px" class="pay">
        <el-form-item label="订单号">
          {{ orderNum }}
        </el-form-item>
        <el-form-item label="保费金额">
          {{ insureFee }}
        </el-form-item>
        <el-form-item label="支付密码" prop="walletPassword">
          <el-input placeholder="请输入密码" v-model="payForm.walletPassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <p style="color:red">支付密码不是6位数字的客户，请在【管理中心】-【支付密码】中，尽快重新设置支付密码为6位数字</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="payVisible = false">取 消</el-button>
        <el-button type="primary" @click="payDetermine">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="支付" :visible.sync="mulPayVisible" width="30%" :before-close="() => mulPayVisible = false" v-loading="loading">
      <el-form :model="payForm" :rules="payRules" ref="payForm" label-width="90px" class="pay">
        <el-form-item label="支付密码" prop="walletPassword">
          <el-input placeholder="请输入支付密码" :maxlength="6" v-model="payForm.walletPassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <p style="color:red">支付密码不是6位数字的客户，请在【管理中心】-【支付密码】中，尽快重新设置支付密码为6位数字</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="mulPayVisible = false">取 消</el-button>
        <el-button type="primary" @click="mulPayDetermine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      mulPayVisible: false,
      loading: false,
      pageSize: 10,
      total: 0,
      pageNum: 1,
      reverse: true,
      dialogVisible: false,
      tableHeight: 0,
      currentPage4: 4,
      isShow: false,
      formInline: {
        containerNumber: "",
        // tagInvoiceWaybill: ""
      },
      tableData: [],

      payVisible: false,
      payForm: { walletPassword: "" },
      payRules: {
        walletPassword: [
          { required: true, message: "请输入支付密码", trigger: "blur" },
          { max: 6, message: "长度在6个字符", trigger: "blur" },
        ],
      },
      orderNum: "",
      insureFee: "",
      multipleSelection: [],
    };
  },
  methods: {
    mulPayDetermine() {
      const orderNums = [];
      this.multipleSelection.forEach((item) => {
        orderNums.push(item.orderNum);
      });
      const params = {
        orderNums,
        walletPassword: this.payForm.walletPassword,
      };
      this.$refs.payForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          this.$post("/warranty/insure/mergePay", params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message.success("支付成功");
                this.payForm.walletPassword = "";
                this.mulPayVisible = false;
                this.getTableData();
              } else if (res && res.code * 1 === 500) {
                if (res.msg === "个人账户余额不足") {
                  this.$confirm(
                    "个人账户余额不足,请充值,充值后可在待支付里继续支付",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      type: "warning",
                      showClose: false,
                      showCancelButton: false,
                    }
                  ).then(() => {
                    this.$router.push({
                      path: "/myWallet",
                    });
                  });
                } else {
                  this.payForm.walletPassword = "";
                }
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    payClick() {
      this.mulPayVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    payDetermine() {
      const params = {
        orderNum: this.orderNum,
        walletPassword: this.payForm.walletPassword,
      };
      this.$refs.payForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          this.$post("/warranty/insure/pay", params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message.success("支付成功");
                this.payForm.walletPassword = "";
                this.orderNum = "";
                this.insureFee = "";
                this.payVisible = false;
                this.getTableData();
              } else if (res && res.code * 1 === 500) {
                if (res.msg === "个人账户余额不足") {
                  this.$confirm(
                    "个人账户余额不足,请充值,充值后可在待支付里继续支付",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      type: "warning",
                      showClose: false,
                      showCancelButton: false,
                    }
                  ).then(() => {
                    this.$router.push({
                      path: "/myWallet",
                    });
                  });
                } else {
                  this.payForm.walletPassword = "";
                }
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    deleteClick(row) {
      this.$toConfirm("是否确认删除", "warning", "提示")
        .then(() => {
          this.delConfirm(row);
        })
        .catch(() => {
          //
        });
    },
    delConfirm(row) {
      this.$post("/warranty/insure/removeWaitPay", {
        orderNum: row.orderNum,
      }).then((res) => {
        if (res && res.code * 1 === 200) {
          this.$message.success("删除成功");
          this.getTableData();
        }
      });
    },
    viewClick(item) {
      this.$router.push({
        path: "/temp1",
        query: {
          warrantyDarftId: item.warrantyDarftId,
          insureInfoId: item.insureInfoId,
          templateName: item.templateName,
          isDetail: 1,
        },
      });
    },
    paymentClick(item) {
      this.payVisible = true;
      this.orderNum = item.orderNum;
      this.insureFee = item.insureFee;
    },
    handleClose() {},
    trackClick() {},
    more() {
      this.isShow = !this.isShow;
      this.setTableHeight();
    },
    onSubmit() {
      this.pageNum = 1;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getTableData();
    },
    setTableHeight() {
      this.$nextTick(() => {
        const bodyHeight = window.innerHeight;
        const queryHeight =
          this.$refs.form &&
          this.$refs.form.$el &&
          this.$refs.form.$el.offsetHeight;
        this.tableHeight = bodyHeight - queryHeight - 210 + "px";
      });
    },
    getTableData() {
      const params = {
        ...this.formInline,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      this.$get("/warranty/insure/waitPay", params).then((res) => {
        if (res) {
          this.tableData = res.rows;
          this.total = res.total;
        } else {
          this.tableData = [];
          this.total = 0;
        }
      });
    },
  },
  mounted() {
    this.setTableHeight();
    window.addEventListener("resize", this.setTableHeight);
    this.getTableData();
  },
};
</script>
<style scoped lang="less">
.time {
  /deep/.el-form-item__label {
    line-height: 20px;
  }
}
.pay {
  .el-form-item {
    margin-bottom: 12px;
  }
}
</style>